package tako.wisers.com.android.apis.handler;

import tako.wisers.com.android.apis.activity.Tako;
import tako.wisers.com.android.apis.adapter.ListManager;
import tako.wisers.com.android.apis.event.Event;
import tako.wisers.com.android.apis.event.EventBus;
import tako.wisers.com.android.apis.parser.StationXMLParser;
import tako.wisers.com.android.apis.parser.xml.item.ButtonItem;
import tako.wisers.com.android.apis.util.Global;

public class ListSwitchHandler extends TakoHandler {
	
	private Event mEvent;
	private final String NEWS_ID = "news";

	public ListSwitchHandler(Tako tako) {
		super(tako);
	}

	public void execute(Event event) {
		mEvent = event;
		if (tako.getBottomDisplay() != Global.LIST_DISPLAY) {
			tako.setBottomDisplay(Global.LIST_DISPLAY);
			ListManager listM = ListManager.getInstance(tako);
			if (listM.size() == 0 && event.getType() == Event.EVENT_CLICK) {
				mEvent.setDismissDialog(false);
				StationXMLParser stationParser = StationXMLParser.getInstance();
				ButtonItem item = stationParser.getButtonById(NEWS_ID);
				Event sEvent = new Event(item.getId(), Event.EVENT_CLICK, true);
				EventBus.getInstance().executeEvent(sEvent);
			}
			mhandler.sendMessage(mhandler.obtainMessage());
		}
	}
	
	private android.os.Handler mhandler = new android.os.Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			tako.switchDisplay(Global.LIST_DISPLAY);
			if(mEvent.isDismissDialog()){
				tako.dismissDialog();
			}
		}
	};

}
